package com.ruoyi.cbs.project.service.impl;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

import com.ruoyi.cbs.approval.domain.ApprovalConfig;
import com.ruoyi.cbs.approval.domain.ApprovalResult;
import com.ruoyi.cbs.approval.mapper.ApprovalConfigMapper;
import com.ruoyi.cbs.approval.mapper.ApprovalResultMapper;
import com.ruoyi.cbs.project.domain.Project;
import com.ruoyi.cbs.project.domain.vo.StartStopProjectProcessReqVO;
import com.ruoyi.cbs.project.mapper.ProjectMapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.project.*;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cbs.project.mapper.ProjectStopMapper;
import com.ruoyi.cbs.project.domain.ProjectStop;
import com.ruoyi.cbs.project.service.IProjectStopService;

/**
 * 项目停工申请Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-21
 */
@Service
public class ProjectStopServiceImpl implements IProjectStopService {
    @Autowired
    private ProjectStopMapper projectStopMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private ApprovalConfigMapper approvalConfigMapper;
    @Autowired
    private ApprovalResultMapper approvalResultMapper;

    /**
     * 查询项目停工申请
     *
     * @param id 项目停工申请主键
     * @return 项目停工申请
     */
    @Override
    public ProjectStop selectProjectStopById(Long id) {
        return projectStopMapper.selectProjectStopById(id);
    }

    /**
     * 查询项目停工申请列表
     *
     * @param projectStop 项目停工申请
     * @return 项目停工申请
     */
    @Override
    public List<ProjectStop> selectProjectStopList(ProjectStop projectStop) {
        return projectStopMapper.selectProjectStopList(projectStop);
    }

    /**
     * 新增项目停工申请
     *
     * @param projectStop 项目停工申请
     * @return 结果
     */
    @Override
    public int insertProjectStop(ProjectStop projectStop) {
        projectStop.setCreateTime(DateUtils.getNowDate());
        return projectStopMapper.insertProjectStop(projectStop);
    }

    /**
     * 修改项目停工申请
     *
     * @param projectStop 项目停工申请
     * @return 结果
     */
    @Override
    public int updateProjectStop(ProjectStop projectStop) {
        projectStop.setUpdateTime(DateUtils.getNowDate());
        return projectStopMapper.updateProjectStop(projectStop);
    }

    /**
     * 批量删除项目停工申请
     *
     * @param ids 需要删除的项目停工申请主键
     * @return 结果
     */
    @Override
    public int deleteProjectStopByIds(Long[] ids) {
        return projectStopMapper.deleteProjectStopByIds(ids);
    }

    /**
     * 删除项目停工申请信息
     *
     * @param id 项目停工申请主键
     * @return 结果
     */
    @Override
    public int deleteProjectStopById(Long id) {
        return projectStopMapper.deleteProjectStopById(id);
    }

    @Override
    public int startStopProjectProcess(LoginUser user, StartStopProjectProcessReqVO startStopProjectProcessReqVO) {
        //获取当前准备停工的项目
        Long projectId = startStopProjectProcessReqVO.getProjectId();
        Project project = projectMapper.selectProjectById(projectId);
        int result = 0;
        //如果登陆人是业主则直接发起停工,如果不为业主则走审批流程
        if (user.getUserId() != project.getOwnerLeaderId()) {
            //判断当前登录用户是否为监理工程师
            String supervisionEngineer = project.getSupervisionEngineer();
            int step = 1;
            int excuteStatus = 1;

            if (user.getUsername().equals(supervisionEngineer)) {
                //如果当前用户为监理工程师 则走审批流程5
                List<ApprovalConfig> approvalConfigs = approvalConfigMapper.selectApprovalConfigByType(ApprovalTypeEnum.STOP_WORK_APPLY_TWO.getCode());
                for (ApprovalConfig config : approvalConfigs) {
                    if (config.getCode().length() < 6) {
                        continue;
                    }
                    ApprovalResult approvalResult = new ApprovalResult();
                    approvalResult.setTargetId(projectId);
                    approvalResult.setTargetName(project.getProjectName());
                    approvalResult.setTargetType(project.getProjectType());
                    approvalResult.setConfigId(config.getId());
                    approvalResult.setApprovalType(ApprovalTypeEnum.STOP_WORK_APPLY_TWO.getCode());
                    approvalResult.setApprovalStep(String.valueOf(step));
                    if (config.getName().equals(ApprovalNodeEnum.APPROVED.getInfo())) {
                        if (StringUtils.isNotEmpty(project.getSupervisionEngineer())) {
                            approvalResult.setApprovalUserName(project.getSupervisionEngineer());
                        } else {
                            continue;
                        }
                    }
                    if (config.getName().equals(ApprovalNodeEnum.REJECTED.getInfo())) {
                        if (StringUtils.isNotEmpty(project.getOwnerLeader())) {
                            approvalResult.setApprovalUserId(project.getOwnerLeaderId());
                            approvalResult.setApprovalUserName(project.getOwnerLeader());
                        } else {
                            continue;
                        }
                    }
                    approvalResult.setApprovalStatus(ApprovalStatusEnum.WAITING.getCode());
                    approvalResult.setRequireSign(startStopProjectProcessReqVO.getRequireSign());
                    approvalResult.setStatus("1");
                    approvalResult.setDelFlag("0");
                    approvalResult.setCreateTime(DateUtils.getNowDate());
                    if (excuteStatus == 1) {
                        approvalResult.setExecuteStatus(ApprovalExcuteStatusEnum.WORKING.getCode());
                    } else {
                        approvalResult.setExecuteStatus(ApprovalExcuteStatusEnum.UNSTART.getCode());
                    }
                    result = result + approvalResultMapper.insertApprovalResult(approvalResult);
                    step++;
                    excuteStatus++;
                }
            } else {
                //如果当前用户不为监理工程师 则走审批流程4
                List<ApprovalConfig> approvalConfigs = approvalConfigMapper.selectApprovalConfigByType(ApprovalTypeEnum.STOP_WORK_APPLY_ONE.getCode());
                for (ApprovalConfig config : approvalConfigs) {
                    if (config.getCode().length() < 6) {
                        continue;
                    }
                    ApprovalResult approvalResult = new ApprovalResult();
                    approvalResult.setTargetId(projectId);
                    approvalResult.setTargetName(project.getProjectName());
                    approvalResult.setTargetType(project.getProjectType());
                    approvalResult.setConfigId(config.getId());
                    approvalResult.setApprovalType(ApprovalTypeEnum.STOP_WORK_APPLY_TWO.getCode());
                    approvalResult.setApprovalStep(String.valueOf(step));
                    if (config.getName().equals(ApprovalNodeEnum.REJECTED.getInfo())) {
                        if (StringUtils.isNotEmpty(project.getOwnerLeader())) {
                            approvalResult.setApprovalUserId(project.getOwnerLeaderId());
                            approvalResult.setApprovalUserName(project.getOwnerLeader());
                        } else {
                            continue;
                        }
                    }
                    approvalResult.setApprovalStatus(ApprovalStatusEnum.WAITING.getCode());
                    approvalResult.setRequireSign(startStopProjectProcessReqVO.getRequireSign());
                    approvalResult.setStatus("1");
                    approvalResult.setDelFlag("0");
                    approvalResult.setCreateTime(DateUtils.getNowDate());
                    if (excuteStatus == 1) {
                        approvalResult.setExecuteStatus(ApprovalExcuteStatusEnum.WORKING.getCode());
                    } else {
                        approvalResult.setExecuteStatus(ApprovalExcuteStatusEnum.UNSTART.getCode());
                    }
                    result = result + approvalResultMapper.insertApprovalResult(approvalResult);
                    step++;
                    excuteStatus++;
                }
            }
        }
        //保存停工信息
        ProjectStop projectStop = new ProjectStop();
        projectStop.setApplicationNo(UUID.randomUUID().toString().substring(0,49));
        projectStop.setProjectId(projectId);
        projectStop.setProjectName(project.getProjectName());
        projectStop.setContractorId(project.getContractorId());
        projectStop.setContractorName(project.getContractorName());
        projectStop.setStatus(StopProjectStatusEnum.APPROVING.getCode());
        projectStop.setDelFlag("0");
        projectStop.setCreateTime(DateUtils.getNowDate());
        result = result + projectStopMapper.insertProjectStop(projectStop);
        return result;
    }
}
